package com.dlc.shop.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dlc.shop.bean.enums.*;
import com.dlc.shop.bean.model.Delivery;
import com.dlc.shop.bean.model.Order;
import com.dlc.shop.bean.model.OrderItem;
import com.dlc.shop.bean.model.UserAddrOrder;
import com.dlc.shop.bean.param.OrderParam;
import com.dlc.shop.bean.vo.UnDeliveryOrderExcelVO;
import com.dlc.shop.common.exception.YamiShopBindException;
import com.dlc.shop.common.i18n.I18nMessage;
import com.dlc.shop.common.util.PoiExcelUtil;
import com.dlc.shop.common.util.PrincipalUtil;
import com.dlc.shop.dao.OrderMapper;
import com.dlc.shop.service.DeliveryService;
import com.dlc.shop.service.OrderExcelService;
import com.dlc.shop.service.OrderService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Pineapple
 * @date 2021/7/21 15:37
 */
@Service
@RequiredArgsConstructor
public class OrderExcelServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderExcelService {

    private final OrderMapper orderMapper;
    private final DeliveryService deliveryService;
    private final OrderService orderService;
    private final static int MIN_ROW = 2;
    private final static int FLOW_ID_MIN = 4;
    private final static int FLOW_ID_MAX = 30;
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public void soldExcel(OrderParam orderParam, HttpServletResponse response) {
        if (Objects.isNull(orderParam.getLang())){
            orderParam.setLang(I18nMessage.getLang());
        }
        List<Order> orders = orderMapper.listOrdersDetailByOrderInfo(orderParam);
        //通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getBigWriter();
        // 待发货
        String[] headers = {
                I18nMessage.getMessage("yami.order.excel.header.seq"),
                I18nMessage.getMessage("yami.order.excel.header.orderNum"),
                I18nMessage.getMessage("yami.order.excel.header.placeTime"),
                I18nMessage.getMessage("yami.order.excel.header.receiver"),
                I18nMessage.getMessage("yami.order.excel.header.phone"),
                I18nMessage.getMessage("yami.order.excel.header.addr"),
                I18nMessage.getMessage("yami.order.excel.header.prodName"),
                I18nMessage.getMessage("yami.order.excel.header.prodNum"),
                I18nMessage.getMessage("yami.order.excel.header.prodStatus"),
                I18nMessage.getMessage("yami.order.excel.header.payable"),
                I18nMessage.getMessage("yami.order.excel.header.shipCost"),
                I18nMessage.getMessage("yami.order.excel.header.actualPay"),
                I18nMessage.getMessage("yami.order.excel.header.orderStatus"),
        };
        List<String> headerList = new ArrayList<>(Arrays.asList(headers));
        if (!Objects.equals(orderParam.getOrderType(), OrderType.SCORE.value())) {
            logger.info("{}类型订单导出Excle！！", orderParam.getOrderType());
            headerList.add(I18nMessage.getMessage("yami.order.excel.header.saleStatus"));
        }
        writer.merge(headerList.size() - 1, I18nMessage.getMessage("yami.order.excel.title"));
        writer.writeRow(headerList);

        Sheet sheet = writer.getSheet();
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 20 * 256);
        sheet.setColumnWidth(3, 20 * 256);
        sheet.setColumnWidth(4, 60 * 256);
        sheet.setColumnWidth(5, 60 * 256);

        //商品状态 '处理退款状态:(1.买家申请 2.卖家接受 3.买家发货 4.卖家收货 5.退款成功 6.买家撤回申请 7.商家拒绝 -1.退款关闭)详情见ReturnMoneyStsType',
        String[] productStatuses = {
                "",
                I18nMessage.getMessage("yami.prod.status.apply"),
                I18nMessage.getMessage("yami.prod.status.accept"),
                I18nMessage.getMessage("yami.prod.status.delivery"),
                I18nMessage.getMessage("yami.prod.status.receive"),
                I18nMessage.getMessage("yami.prod.status.refundSuccess"),
                I18nMessage.getMessage("yami.prod.status.cancelApply"),
                I18nMessage.getMessage("yami.prod.status.reject")
        };
        // 处理订单数据
        handleOrders(orders, writer, productStatuses);
        PoiExcelUtil.writeExcel(response, writer);
    }

    private void handleOrders(List<Order> orders, ExcelWriter writer, String[] productStatuses){
        int row = 1;
        int seq = 1;
        String receiveMobile;
        for (Order dbOrder : orders) {
            UserAddrOrder addr = dbOrder.getUserAddrOrder();
            String addrInfo = "";
            if (Objects.nonNull(addr)) {
                addrInfo = addr.getProvince() + addr.getCity() + addr.getArea() + addr.getAddr();
            }
            if (Objects.equals(dbOrder.getDvyType(), DvyType.STATION.value())) {
                addrInfo = I18nMessage.getMessage("yami.order.excel.selfOrder");
            }
            List<OrderItem> orderItems = dbOrder.getOrderItems();
            int firstRow = row + 1;
            int lastRow = row + orderItems.size();
            int col = -1;
            // 序号
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, seq++);
            // 订单编号
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getOrderNumber());
            // 下单时间
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getCreateTime());
            // 收件人
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getReceiverName());
            // "手机"
            receiveMobile = dbOrder.getReceiverMobile();
            if (Objects.nonNull(receiveMobile)) {
                receiveMobile = PhoneUtil.hideBetween(receiveMobile).toString();
            }
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, receiveMobile);
            // "收货地址"
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, addrInfo);
            int prodNameCol = ++col;
            int prodCountCol = ++col;
            int prodStatusCol = ++col;
            boolean isPay = Objects.equals(dbOrder.getIsPayed(), 1);
            for (OrderItem orderItem : orderItems) {
                row++;
                // 商品名称
                writer.writeCellValue(prodNameCol, row, orderItem.getProdName()+orderItem.getSkuName());
                // 数量
                writer.writeCellValue(prodCountCol, row, orderItem.getProdCount());
                //商品状态 '处理退款状态:(1.买家申请 2.卖家接受 3.买家发货 4.卖家收货 5.退款成功 6.买家撤回申请 7.商家拒绝 -1.退款关闭)详情见ReturnMoneyStsType',
                String productStatus = "";
                if(Objects.nonNull(orderItem.getReturnMoneySts())){
                    productStatus = productStatuses[orderItem.getReturnMoneySts()];
                } else if (!isPay) {
                    productStatus = I18nMessage.getMessage("yami.prod.status.unpay");
                } else if(Objects.equals(orderItem.getStatus(), -1)) {
                    productStatus = I18nMessage.getMessage("yami.prod.status.waitDelivery");
                } else if(Objects.equals(orderItem.getStatus(), 0)){
                    productStatus = I18nMessage.getMessage("yami.prod.status.AllDelivery");
                } else if (Objects.equals(orderItem.getStatus(), 2)) {
                    productStatus = I18nMessage.getMessage("yami.prod.status.partialDelivery");
                }
                writer.writeCellValue(prodStatusCol, row, productStatus);
            }
            // 订单应付
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getTotal());
            // 订单运费
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getFreightAmount());
            // 订单实付
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, dbOrder.getActualTotal());
            //订单状态
            String[] prodStatuses = {
                    "",
                    I18nMessage.getMessage("yami.order.status.waitPay"),
                    I18nMessage.getMessage("yami.order.status.waitDelivery"),
                    I18nMessage.getMessage("yami.order.status.waitReceive"),
                    I18nMessage.getMessage("yami.order.status.waitComm"),
                    I18nMessage.getMessage("yami.order.status.success"),
                    I18nMessage.getMessage("yami.order.status.fail"),
                    I18nMessage.getMessage("yami.order.status.waitGroup"),
                    ""
            };
            String status = prodStatuses[dbOrder.getStatus()];
            //订单退款状态（1:申请退款 2:退款成功 3:部分退款成功 4:退款失败）
            String refundStatus = "";
            String[] refundStatuses = {
                    "",
                    I18nMessage.getMessage("yami.order.refund.status.apply"),
                    I18nMessage.getMessage("yami.order.refund.status.success"),
                    I18nMessage.getMessage("yami.order.refund.status.partSuccess"),
                    I18nMessage.getMessage("yami.order.refund.status.fail")
            };
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, status);
            if (!Objects.isNull(dbOrder.getRefundStatus())) {
                refundStatus = refundStatuses[dbOrder.getRefundStatus()];
            } else {
                refundStatus = I18nMessage.getMessage("yami.order.refund.status.noSale");
            }
            if (!Objects.equals(dbOrder.getOrderType(), OrderType.SCORE.value())) {
                PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, refundStatus);
            }
        }
    }

    @Override
    public void unDeliveryOrderExcel(OrderParam orderParam, HttpServletResponse response) {
        List<UnDeliveryOrderExcelVO> orderExcelList = orderMapper.listUnDeliveryOrderExcel(orderParam);
        handleExportGiveawayList(orderExcelList);
        //创建writer
        ExcelWriter writer = ExcelUtil.getBigWriter();
        String[] orderHeaders = {
                I18nMessage.getMessage("yami.delivery.order.excel.header.orderNum"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.deliveryMethod"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.dvyCompany"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.dvyNum"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.receiver"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.phone"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.addr"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.prodName"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.skuName"),
                I18nMessage.getMessage("yami.delivery.order.excel.header.prodNum")
        };
        List<String> orderHeaderList = new ArrayList<>(Arrays.asList(orderHeaders));
        if (!Objects.equals(orderParam.getOrderType(), OrderType.SCORE.value())) {
            logger.info("{}类型订单导出Excel！！", orderParam.getOrderType());
            orderHeaderList.add(I18nMessage.getMessage("yami.delivery.order.excel.header.giveawayName"));
            orderHeaderList.add(I18nMessage.getMessage("yami.delivery.order.excel.header.giveawayNum"));
        }
        Sheet sheet = writer.getSheet();
        setSheetColumnWidth(sheet);
        writer.merge(orderHeaderList.size() - 1, "待发货订单信息整理");
        writer.writeRow(orderHeaderList);
        //配送方式
        String[] dvyTypes = {"快递配送", "无需快递", "同城配送"};
        if (Objects.equals(orderParam.getOrderType(), OrderType.SCORE.value())) {
            dvyTypes = new String[]{"快递配送", "无需快递"};
        }
        PoiExcelUtil.createDropDownList(sheet, dvyTypes, 2, 50000, 1, 1);
        //快递公司
        List<Delivery> deliveryList = deliveryService.list(new LambdaQueryWrapper<>());
        if (CollUtil.isNotEmpty(deliveryList)) {
            String[] dvyName = new String[deliveryList.size()];
            for (int i = 0; i < deliveryList.size(); i++) {
                dvyName[i] = deliveryList.get(i).getDvyName();
            }
            PoiExcelUtil.createDropDownList(writer.getWorkbook(),sheet, Arrays.asList(dvyName), 2, 50000, 2, 2);
        }
        //写入数据
        int row = 1;
        String mobile;
        for (UnDeliveryOrderExcelVO orderExcel : orderExcelList) {
            // 过滤掉组合商品的主商品,主商品不需要发货
            List<OrderItem> orderItemList = orderExcel.getOrderItemList().stream().filter(orderItem -> !Objects.equals(orderItem.getMold(), ProdMoldEnum.COMBO.value())).toList();
            List<OrderItem> giveawayList = orderItemList.stream().filter(o -> CollUtil.isNotEmpty(o.getGiveawayList())).flatMap(orderItem -> orderItem.getGiveawayList().stream()).toList();
            List<OrderItem> countList = orderItemList.stream().filter(o -> CollUtil.isEmpty(o.getGiveawayList())).toList();
            int firstRow = row + 1;
            int lastRow = row + giveawayList.size() + countList.size();
            int col = -1;
            //订单号
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, orderExcel.getOrderNumber());
            //配送方式
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, DeliveryType.getDescription(orderExcel.getDvyType()));
            //快递公司名称
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, orderExcel.getDvyName());
            //快递单号
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, orderExcel.getDvyFlowId());
            //收货人姓名
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, orderExcel.getReceiver());
            //收货人手机号
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, orderExcel.getMobile());
            //收货地址
            PoiExcelUtil.mergeIfNeed(writer, firstRow, lastRow, ++col, col, orderExcel.getReceivingAddr());
            for (OrderItem orderItem : orderItemList) {
                int itemFirstRow = row + 1;
                int index = 6;
                row++;
                int itemLastRow = CollUtil.isEmpty(orderItem.getGiveawayList()) ? row : itemFirstRow + orderItem.getGiveawayList().size() - 1;
                //商品名称
                PoiExcelUtil.mergeIfNeed(writer, row, itemLastRow, ++index, index, orderItem.getProdName());
                //sku名称
                PoiExcelUtil.mergeIfNeed(writer, row, itemLastRow, ++index, index, orderItem.getSkuName());
                //商品待发货数量
                PoiExcelUtil.mergeIfNeed(writer, row, itemLastRow, ++index, index, orderItem.getChangeNum());
                if (CollUtil.isNotEmpty(orderItem.getGiveawayList()) && !Objects.equals(orderParam.getOrderType(), OrderType.SCORE.value())) {
                    for (OrderItem giveaway : orderItem.getGiveawayList()) {
                        int giveIndex = 9;
                        //赠品名称
                        writer.writeCellValue(++giveIndex, row, giveaway.getProdName());
                        //赠品数量
                        writer.writeCellValue(++giveIndex, row, giveaway.getChangeNum());
                        row++;
                    }
                    row--;
                }
            }
        }
        PoiExcelUtil.writeExcel(response, writer);
    }

    private void setSheetColumnWidth(Sheet sheet) {
        sheet.setColumnWidth(0, 20 * 256);
        sheet.setColumnWidth(1, 20 * 256);
        sheet.setColumnWidth(2, 40 * 256);
        sheet.setColumnWidth(3, 20 * 256);
        sheet.setColumnWidth(4, 20 * 256);
        sheet.setColumnWidth(5, 20 * 256);
        sheet.setColumnWidth(6, 60 * 256);
        sheet.setColumnWidth(7, 80 * 256);
        sheet.setColumnWidth(9, 10 * 256);
        sheet.setColumnWidth(10, 80 * 256);
    }

    private void handleExportGiveawayList(List<UnDeliveryOrderExcelVO> orderExcelList) {
        for (UnDeliveryOrderExcelVO order : orderExcelList) {
            // 处理赠品列表
            Map<Long, List<OrderItem>> giveawayMap = order.getOrderItemList().stream().filter(orderItem -> Objects.equals(orderItem.getActivityType(), OrderActivityType.GIVEAWAY.value())).collect(Collectors.groupingBy(OrderItem::getActivityId));
            if (MapUtil.isNotEmpty(giveawayMap)) {
                Iterator<OrderItem> iterator = order.getOrderItemList().iterator();
                while (iterator.hasNext()) {
                    OrderItem orderItem = iterator.next();
                    if (Objects.nonNull(orderItem.getActivityId())) {
                        iterator.remove();
                    }
                    orderItem.setGiveawayList(giveawayMap.get(orderItem.getOrderItemId()));
                }
            }
        }
    }

    private void handleImportGiveawayList(List<OrderItem> orderItems) {
        // 处理赠品列表
        Map<Long, List<OrderItem>> giveawayMap = orderItems.stream().filter(orderItem -> Objects.equals(orderItem.getActivityType(), OrderActivityType.GIVEAWAY.value())).collect(Collectors.groupingBy(OrderItem::getActivityId));
        if (MapUtil.isNotEmpty(giveawayMap)) {
            Iterator<OrderItem> iterator = orderItems.iterator();
            while (iterator.hasNext()) {
                OrderItem orderItem = iterator.next();
                if (Objects.nonNull(orderItem.getActivityId())) {
                    iterator.remove();
                }
                orderItem.setGiveawayList(giveawayMap.get(orderItem.getOrderItemId()));
            }
        }
    }

    @Override
    public String exportOrderExcel(MultipartFile file, Long shopId, List<UnDeliveryOrderExcelVO> orderList) throws Exception {
        if (!PoiExcelUtil.checkFileMagic(file.getInputStream())) {
            return I18nMessage.getMessage("yami.file.error");
        }
        try (InputStream inputStream = file.getInputStream();
             Workbook workbook = WorkbookFactory.create(inputStream)) {
            //读取工作表
            Sheet sheet = workbook.getSheetAt(0);
            int allRow = sheet.getPhysicalNumberOfRows();
            //无内容
            if (allRow <= MIN_ROW) {
                throw new YamiShopBindException("yami.document.no.content");
            }
            //进行数据校验
            List<Delivery> deliveryList = deliveryService.list(new LambdaQueryWrapper<Delivery>().eq(Delivery::getStatus, 1));
            Map<String, Long> deliveryCompanyMap = deliveryList.stream().collect(Collectors.toMap(Delivery::getDvyName, Delivery::getDvyId));
            int dvyType = 0;
            int step = 1;
            for (int i = 2; i < allRow; i += step) {
                UnDeliveryOrderExcelVO unDeliveryOrderExcelVO = new UnDeliveryOrderExcelVO();
                int trueStep = 1;
                try {
                    trueStep = checkExcel(sheet, i, dvyType, unDeliveryOrderExcelVO, shopId, deliveryCompanyMap, trueStep);
                    step = trueStep;
                } catch (Exception e) {
                    //错误信息提示
                    return "第" + (i + 1) + "行数据有错误," + e.getMessage();
                }
                orderList.add(unDeliveryOrderExcelVO);
            }
            return null;
        } catch (Exception e) {
            return e.getMessage();
        }

    }

    private int checkExcel(Sheet sheet, int i, int dvyType, UnDeliveryOrderExcelVO unDeliveryOrderExcelVO, Long shopId, Map<String, Long> deliveryCompanyMap, int trueStep) {
        Row row = sheet.getRow(i);
        int cellRow = 0;
        String orderNumber = PoiExcelUtil.getRowValue(row, cellRow, false);
        if (Objects.isNull(orderNumber) || orderNumber.length() == 0) {
            //订单不存在
            throw new YamiShopBindException("yami.order.no.exist");
        }
        UnDeliveryOrderExcelVO order = orderService.getOrderAndOrderItemsByOrderNumberAndShopId(orderNumber, shopId);
        if (Objects.isNull(order)) {
            // 订单不存在
            throw new YamiShopBindException("yami.order.no.exist");
        }
        for (OrderItem orderItem : order.getOrderItemList()) {
            boolean unDelivery = Objects.equals(orderItem.getStatus(), 0) || (!Objects.equals(orderItem.getProdCount(), orderItem.getStatus()) && !Objects.equals(orderItem.getStatus(), -1));
            if (unDelivery) {
                //如果有订单项发货了，则不允许批量发货
                throw new YamiShopBindException("yami.order.is.delivery");
            }
        }
        String deliveryType = PoiExcelUtil.getRowValue(row, ++cellRow, false);
        if (Objects.isNull(deliveryType) || deliveryType.length() == 0) {
            //请选择配送方式
            throw new YamiShopBindException("yami.product.dvy.type");
        } else if (Objects.equals(deliveryType, DeliveryType.NO_EXPRESS.getDesc())) {
            dvyType = DeliveryType.NO_EXPRESS.getValue();
        } else if (Objects.equals(deliveryType, DeliveryType.SAME_CITY.getDesc())) {
            dvyType = DeliveryType.SAME_CITY.getValue();
        } else if (Objects.equals(deliveryType, DeliveryType.EXPRESS.getDesc())) {
            dvyType = DeliveryType.EXPRESS.getValue();
        }
        if (Objects.equals(dvyType, DeliveryType.SAME_CITY.getValue()) && !Objects.equals(order.getDvyType(), DeliveryType.SAME_CITY.getValue())) {
            //当前订单不支持同城配送
            throw new YamiShopBindException("yami.order.same.city.error3");
        }
        //导入的配送方式与订单的配送方式不一致
        //订单的配送方式为快递配送时，导入的配送方式可选择快递配送和无需快递，其他情况订单的配送方式与导入的配送方式需保持一致
        if(!Objects.equals(order.getDvyType(), dvyType) && !Objects.equals(order.getDvyType(), DeliveryType.EXPRESS.getValue())){
            throw new YamiShopBindException("yami.order.devType.notMatch");
        }else if(!Objects.equals(order.getDvyType(), dvyType) && Objects.equals(order.getDvyType(), DeliveryType.EXPRESS.getValue())){
            if(!Objects.equals(dvyType, DeliveryType.NO_EXPRESS.getValue())){
                throw new YamiShopBindException("yami.order.devType.notMatch");
            }
        }
        String dvyName = PoiExcelUtil.getRowValue(row, ++cellRow, false);
        String dvyFlowId = PoiExcelUtil.getRowValue(row, ++cellRow, false);
        if (Objects.equals(deliveryType, DeliveryType.EXPRESS.getDesc())) {
            //只有快递需要校验快递信息
            if (Objects.isNull(dvyName) || dvyName.length() == 0) {
                //快递公司名称不能为空
                throw new YamiShopBindException("yami.order.no.dvyName");
            }
            dvyFlowId = StringUtils.deleteWhitespace(dvyFlowId);
            if (StrUtil.isBlank(dvyFlowId) || dvyFlowId.length() < FLOW_ID_MIN || dvyFlowId.length() > FLOW_ID_MAX
                    || !PrincipalUtil.isMatching(PrincipalUtil.WITHOUT_CHINESE, dvyFlowId)) {
                //快递单号格式错误
                throw new YamiShopBindException("yami.order.delivery.number");
            }
            unDeliveryOrderExcelVO.setDvyFlowId(dvyFlowId);
            unDeliveryOrderExcelVO.setDvyName(dvyName);
            unDeliveryOrderExcelVO.setDvyId(deliveryCompanyMap.get(dvyName));
        }
        unDeliveryOrderExcelVO.setOrderNumber(orderNumber);
        unDeliveryOrderExcelVO.setDvyType(dvyType);
        List<OrderItem> orderItems = order.getOrderItemList();
        //处理有赠品的订单项，防止跳过多个表格行
        List<OrderItem> orderItemes = new ArrayList<>();
        orderItemes.addAll(orderItems);
        handleImportGiveawayList(orderItemes);
        //已经全部发货的就不加入到待发货订单项
        orderItems.removeIf(orderItem -> Objects.equals(orderItem.getStatus(), 0));
        orderItemes.removeIf(orderItem -> Objects.equals(orderItem.getStatus(), 0));
        List<OrderItem> giveawayList = orderItemes.stream().filter(o -> CollUtil.isNotEmpty(o.getGiveawayList())).flatMap(orderItem -> orderItem.getGiveawayList().stream()).toList();
        List<OrderItem> countList = orderItemes.stream().filter(o -> CollUtil.isEmpty(o.getGiveawayList())).toList();
        if (orderItemes.size() > 0) {
            trueStep = giveawayList.size() + countList.size();
        }
        unDeliveryOrderExcelVO.setOrderItemList(orderItems);
        return trueStep;
    }
}
